package com.zf.rpc.entity;

import com.zf.rpc.enumeration.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @ProjectName: RPC-zhaofeng
 * @Package: com.zf.simpleImplementation
 * @ClassName: RpcResponse
 * @Author: zf
 * @Description:
 * @Date: 2021/5/6 19:10
 * @Version: 1.0
 */
@Data
@NoArgsConstructor
public class RpcResponse<T> implements Serializable {
    //响应对应的请求号
    private String requestId;

    //响应状态码
    private Integer statusCode;

    //响应状态补充信息
    private String message;

    //响应数据
    private T data;


    public static <T> RpcResponse<T> success(T data, String requestId) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setRequestId(requestId);
        response.setStatusCode(ResponseCode.SUCCESS.getCode());
        response.setData(data);
        return response;
    }
    public static <T> RpcResponse<T> fail(ResponseCode code, String requestId) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setRequestId(requestId);
        response.setStatusCode(code.getCode());
        response.setMessage(code.getMessage());
        return response;
    }
}
